<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/8
 * Time: 16:54
 */

//管理员添加新闻接口
require_once 'db.php';
require_once  'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$title = isset($_POST['title']) ? $_POST['title'] : '';
$content = isset($_POST['content']) ? $_POST['content']: '';
$maker = isset($_POST['maker']) ? $_POST['maker'] : '';
$time = time();

if(empty($title) || empty($content) || empty($maker)) {
    return Response::show(401,'数据不合法');
}

try{
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$sql = "INSERT news VALUES(null,?,?,$time,?)";

$mysqli_stmt = $connect->prepare($sql);

$mysqli_stmt->bind_param('sss',$title,$content,$maker);

$data = array(
    'IsSuccess'=>false,
    'InsertId'=>''
);

//var_dump($mysqli_stmt->execute());

if($mysqli_stmt->execute()) {
//    var_dump($mysqli_stmt);
    $data['IsSuccess'] = true;
    $data['InsertId'] = $mysqli_stmt->insert_id;
    return Response::show(200,'新闻上传成功',$data);
} else {
    if($mysqli_stmt->errno == 1452) {
        return Response::show(400,'没有找到该用户',$data);
    } else {
        return Response::show(404,'新闻上传失败,服务器没响应',$data);
    }
}


